package com.numberone.web.controller.system;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.numberone.common.utils.DateUtils;
import com.numberone.framework.util.ShiroUtils;
import com.numberone.system.mapper.SaqMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.numberone.common.annotation.Log;
import com.numberone.common.enums.BusinessType;
import com.numberone.system.domain.Saq;
import com.numberone.system.service.ISaqService;
import com.numberone.framework.web.base.BaseController;
import com.numberone.common.page.TableDataInfo;
import com.numberone.common.base.AjaxResult;
import com.numberone.common.utils.poi.ExcelUtil;

/**
 * 人员借调申请 信息操作处理
 * 
 * @author numberone
 * @date 2019-06-10
 */
@Controller
@RequestMapping("/system/saq")
public class SaqController extends BaseController
{
    private String prefix = "system/saq";
	
	@Autowired
	private ISaqService saqService;

	@Autowired
	private SaqMapper saqMapper;
	
	@RequiresPermissions("system:saq:view")
	@GetMapping()
	public String saq()
	{
	    return prefix + "/saq";
	}
	
	/**
	 * 查询人员借调申请列表
	 */
	@RequiresPermissions("system:saq:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Saq saq)
	{
		startPage();
        List<Saq> list = saqService.selectSaqList(saq);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出人员借调申请列表
	 */
	@Log(title = "申请表", businessType = BusinessType.EXPORT)
	@RequiresPermissions("system:saq:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Saq saq)
    {
    	List<Saq> list = saqService.selectSaqList(saq);
        ExcelUtil<Saq> util = new ExcelUtil<Saq>(Saq.class);
        return util.exportExcel(list, "申请表");
    }
	
	/**
	 * 新增人员借调申请
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存人员借调申请
	 */
	@RequiresPermissions("system:saq:add")
	@Log(title = "人员借调申请", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Saq saq)
	{
		saq.setCreateBy(ShiroUtils.getLoginName());
		saq.setUserId(ShiroUtils.getUserId());
		saq.setUpdateBy(ShiroUtils.getLoginName());
		//生成申请表编号并保存进数据库
		String dateNo = DateUtils.dateTime2();
//		DecimalFormat df = new DecimalFormat("00");
		saq.setSaqNum(dateNo);
		return toAjax(saqService.insertSaq(saq));
	}

	/**
	 * 修改人员借调申请
	 */
	@GetMapping("/edit/{saqId}")
	public String edit(@PathVariable("saqId") Long saqId, ModelMap mmap)
	{
		Saq saq = saqService.selectSaqById(saqId);
		mmap.put("saq", saq);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存人员借调申请
	 */
	@RequiresPermissions("system:saq:edit")
	@Log(title = "人员借调申请", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Saq saq)
	{

//		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		simpleDateFormat.format(saq.getStartTime());
//		simpleDateFormat.format(saq.getFinishTime());
		return toAjax(saqService.updateSaq(saq));
	}

	@RequiresPermissions("system:saq:commit")
	@Log(title = "人员借调申请",businessType = BusinessType.COMMIT)
	@PostMapping("/commit")
	@ResponseBody
	public AjaxResult commit(Long saqId){

		return toAjax(saqService.commitSaqById(saqId));
	}

	/**
	 * 删除人员借调申请
	 */
	@RequiresPermissions("system:saq:remove")
	@Log(title = "人员借调申请", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(saqService.deleteSaqByIds(ids));
	}
	
}
